home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / io / BufferedOutputStream.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  4.4 KB  |  138 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)BufferedOutputStream.java    1.25 98/04/30
  3.  *
  4.  * Copyright 1994-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. /**
  18.  * The class implements a buffered output stream. By setting up such 
  19.  * an output stream, an application can write bytes to the underlying 
  20.  * output stream without necessarily causing a call to the underlying 
  21.  * system for each byte written. The data is written into an internal 
  22.  * buffer, and then written to the underlying stream if the buffer 
  23.  * reaches its capacity, the buffer output stream is closed, or the 
  24.  * buffer output stream is explicitly flushed. 
  25.  *
  26.  * @author  Arthur van Hoff
  27.  * @version 1.25, 04/30/98
  28.  * @since   JDK1.0
  29.  */
  30. public 
  31. class BufferedOutputStream extends FilterOutputStream {
  32.     /**
  33.      * The internal buffer where data is stored. 
  34.      */
  35.     protected byte buf[];
  36.  
  37.     /**
  38.      * The number of valid bytes in the buffer. This value is always 
  39.      * in the range <tt>0</tt> through <tt>buf.length</tt>; elements 
  40.      * <tt>buf[0]</tt> through <tt>buf[count-1]</tt> contain valid 
  41.      * byte data.
  42.      */
  43.     protected int count;
  44.     
  45.     /**
  46.      * Creates a new buffered output stream to write data to the 
  47.      * specified underlying output stream with a default 512-byte 
  48.      * buffer size.
  49.      *
  50.      * @param   out   the underlying output stream.
  51.      */
  52.     public BufferedOutputStream(OutputStream out) {
  53.     this(out, 512);
  54.     }
  55.  
  56.     /**
  57.      * Creates a new buffered output stream to write data to the 
  58.      * specified underlying output stream with the specified buffer 
  59.      * size. 
  60.      *
  61.      * @param   out    the underlying output stream.
  62.      * @param   size   the buffer size.
  63.      * @exception IllegalArgumentException if size <= 0.
  64.      */
  65.     public BufferedOutputStream(OutputStream out, int size) {
  66.     super(out);
  67.         if (size <= 0) {
  68.             throw new IllegalArgumentException("Buffer size <= 0");
  69.         }
  70.     buf = new byte[size];
  71.     }
  72.  
  73.     /** Flush the internal buffer */
  74.     private void flushBuffer() throws IOException {
  75.         if (count > 0) {
  76.         out.write(buf, 0, count);
  77.         count = 0;
  78.         }
  79.     }
  80.  
  81.     /**
  82.      * Writes the specified byte to this buffered output stream. 
  83.      *
  84.      * @param      b   the byte to be written.
  85.      * @exception  IOException  if an I/O error occurs.
  86.      */
  87.     public synchronized void write(int b) throws IOException {
  88.     if (count >= buf.length) {
  89.         flushBuffer();
  90.     }
  91.     buf[count++] = (byte)b;
  92.     }
  93.  
  94.     /**
  95.      * Writes <code>len</code> bytes from the specified byte array 
  96.      * starting at offset <code>off</code> to this buffered output stream.
  97.      *
  98.      * <p> Ordinarily this method stores bytes from the given array into this
  99.      * stream's buffer, flushing the buffer to the underlying output stream as
  100.      * needed.  If the requested length is at least as large as this stream's
  101.      * buffer, however, then this method will flush the buffer and write the
  102.      * bytes directly to the underlying output stream.  Thus redundant
  103.      * <code>BufferedOutputStream</code>s will not copy data unnecessarily.
  104.      *
  105.      * @param      b     the data.
  106.      * @param      off   the start offset in the data.
  107.      * @param      len   the number of bytes to write.
  108.      * @exception  IOException  if an I/O error occurs.
  109.      */
  110.     public synchronized void write(byte b[], int off, int len) throws IOException {
  111.     if (len >= buf.length) {
  112.         /* If the request length exceeds the size of the output buffer,
  113.                flush the output buffer and then write the data directly.
  114.                In this way buffered streams will cascade harmlessly. */
  115.         flushBuffer();
  116.         out.write(b, off, len);
  117.         return;
  118.     }
  119.     if (len > buf.length - count) {
  120.         flushBuffer();
  121.     }
  122.     System.arraycopy(b, off, buf, count, len);
  123.     count += len;
  124.     }
  125.  
  126.     /**
  127.      * Flushes this buffered output stream. This forces any buffered 
  128.      * output bytes to be written out to the underlying output stream. 
  129.      *
  130.      * @exception  IOException  if an I/O error occurs.
  131.      * @see        java.io.FilterOutputStream#out
  132.      */
  133.     public synchronized void flush() throws IOException {
  134.         flushBuffer();
  135.     out.flush();
  136.     }
  137. }
  138.